<template>
  <div>
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="商品名称">
        <el-input v-model="form.name" placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品信息">
        <el-input v-model="form.info" placeholder="请输入商品信息"></el-input>
      </el-form-item>
      <el-form-item label="商品价格">
        <el-input v-model="form.price" placeholder="请输入商品信息"></el-input>
      </el-form-item>
      <el-form-item label="是否上架">
        <el-switch v-model="form.onSale"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button  @click="add" type="success" round>添加商品</el-button>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <div>
      <el-table :data="items" border>
        <el-table-column prop="name" label="商品名称"></el-table-column>
        <el-table-column prop="info" label="商品信息"></el-table-column>
        <el-table-column prop="price" label="商品价格"></el-table-column>
        <el-table-column prop="onSale" label="商品状态" :formatter="formatOnSale">
        </el-table-column>
        <el-table-column prop="price" label="上架商品">
          <template slot-scope="scope">
            <el-button v-if="scope.row.onSale===0" @click="up(scope.row.itemId)" type="primary" round>上架商品</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="下架商品">
          <template slot-scope="scope">
            <el-button v-if="scope.row.onSale===1" @click="down(scope.row.itemId)" type="warning" round>下架商品</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {getSessionStorage} from "@/common";
import axios from "axios";
import qs from "qs";

export default {
  name: "ItemManage",
  data(){
    return{
      shopId: this.$route.query.shopId,
      form:{},
      items:[]
    }
  },
  created() {
    this.user = getSessionStorage('user')
    console.log(this.shopId)
    axios.post("/item/queryAll", qs.stringify({
      shopId: this.shopId
    })).then(response => {
      this.items = response.data.data
      console.log(this.items)
    })
  },
  methods:{
    add(){
      axios.post("/item/save", qs.stringify({
        name: this.form.name,
        info: this.form.info,
        price: this.form.price,
        onSale: this.form.onSale ? 1 : 0,
        shopId: this.shopId
      })).then(() => {
        alert('商品添加成功')
      })
    },
    formatOnSale(row, column, cellValue) {
      if (cellValue === 0) {
        return "未上架";
      } else if (cellValue === 1) {
        return "已上架";
      }
    },
    up(itemId){
      console.log(itemId)
      axios.post("/item/up", qs.stringify({
        itemId: itemId
      })).then(() => {
        alert('商品上架成功')
        window.location.reload
      })
    },
    down(itemId){
      axios.post("/item/down", qs.stringify({
        itemId: itemId
      })).then(() => {
        alert('商品下架成功')
        window.location.reload
      })
    }
  },
}
</script>